﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Rendering.GraphicsFence
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.CompilerServices;
using UnityEngine.Bindings;
using UnityEngine.Scripting;

#nullable disable
namespace UnityEngine.Rendering
{
  /// <summary>
  ///   <para>Used to manage synchronisation between tasks on async compute queues and the graphics queue.</para>
  /// </summary>
  [NativeHeader("Runtime/Graphics/GPUFence.h")]
  [UsedByNativeCode]
  public struct GraphicsFence
  {
    internal IntPtr m_Ptr;
    internal int m_Version;
    internal GraphicsFenceType m_FenceType;

    internal static SynchronisationStageFlags TranslateSynchronizationStageToFlags(
      SynchronisationStage s)
    {
      return s == SynchronisationStage.VertexProcessing ? SynchronisationStageFlags.VertexProcessing : SynchronisationStageFlags.PixelProcessing;
    }

    /// <summary>
    ///   <para>true if GPU execution has passed the processing point where you inserted the GraphicsFence, otherwise false.</para>
    /// </summary>
    public bool passed
    {
      get
      {
        this.Validate();
        if (!SystemInfo.supportsGraphicsFence)
          throw new NotSupportedException("Cannot determine if this GraphicsFence has passed as this platform has not implemented GraphicsFences.");
        if (this.m_FenceType == GraphicsFenceType.AsyncQueueSynchronisation && !SystemInfo.supportsAsyncCompute)
          throw new NotSupportedException("Cannot determine if this AsyncQueueSynchronisation GraphicsFence has passed as this platform does not support async compute.");
        return !this.IsFencePending() || GraphicsFence.HasFencePassed_Internal(this.m_Ptr);
      }
    }

    [FreeFunction("GPUFenceInternals::HasFencePassed_Internal")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern bool HasFencePassed_Internal(IntPtr fencePtr);

    internal void InitPostAllocation()
    {
      if (this.m_Ptr == IntPtr.Zero)
      {
        if (SystemInfo.supportsGraphicsFence)
          throw new NullReferenceException("The internal fence ptr is null, this should not be possible for fences that have been correctly constructed using Graphics.CreateGraphicsFence() or CommandBuffer.CreateGraphicsFence()");
        this.m_Version = this.GetPlatformNotSupportedVersion();
      }
      else
        this.m_Version = GraphicsFence.GetVersionNumber(this.m_Ptr);
    }

    internal bool IsFencePending()
    {
      return !(this.m_Ptr == IntPtr.Zero) && this.m_Version == GraphicsFence.GetVersionNumber(this.m_Ptr);
    }

    internal void Validate()
    {
      if (this.m_Version == 0 || SystemInfo.supportsGraphicsFence && this.m_Version == this.GetPlatformNotSupportedVersion())
        throw new InvalidOperationException("This GraphicsFence object has not been correctly constructed see Graphics.CreateGraphicsFence() or CommandBuffer.CreateGraphicsFence()");
    }

    private int GetPlatformNotSupportedVersion() => -1;

    [NativeThrows]
    [FreeFunction("GPUFenceInternals::GetVersionNumber")]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static extern int GetVersionNumber(IntPtr fencePtr);
  }
}
